package com.sugar.follow.up.module.business.controller.admin.publicityeducationcls;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.follow.up.framework.common.pojo.PageParam;
import com.sugar.follow.up.framework.common.pojo.PageResult;
import com.sugar.follow.up.framework.common.pojo.CommonResult;
import com.sugar.follow.up.framework.common.util.object.BeanUtils;
import static com.sugar.follow.up.framework.common.pojo.CommonResult.success;

import com.sugar.follow.up.framework.excel.core.util.ExcelUtils;

import com.sugar.follow.up.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.follow.up.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.follow.up.module.business.controller.admin.publicityeducationcls.vo.*;
import com.sugar.follow.up.module.business.dal.dataobject.publicityeducationcls.PublicityEducationClsDO;
import com.sugar.follow.up.module.business.service.publicityeducationcls.PublicityEducationClsService;

@Tag(name = "管理后台 - 宣教分类")
@RestController
@RequestMapping("/business/publicity-education-cls")
@Validated
public class PublicityEducationClsController {

    @Resource
    private PublicityEducationClsService publicityEducationClsService;

    @PostMapping("/create")
    @Operation(summary = "创建宣教分类")
    @PreAuthorize("@ss.hasPermission('business:publicity-education-cls:create')")
    public CommonResult<Long> createPublicityEducationCls(@Valid @RequestBody PublicityEducationClsSaveReqVO createReqVO) {
        return success(publicityEducationClsService.createPublicityEducationCls(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新宣教分类")
    @PreAuthorize("@ss.hasPermission('business:publicity-education-cls:update')")
    public CommonResult<Boolean> updatePublicityEducationCls(@Valid @RequestBody PublicityEducationClsSaveReqVO updateReqVO) {
        publicityEducationClsService.updatePublicityEducationCls(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除宣教分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:publicity-education-cls:delete')")
    public CommonResult<Boolean> deletePublicityEducationCls(@RequestParam("id") Long id) {
        publicityEducationClsService.deletePublicityEducationCls(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除宣教分类")
                @PreAuthorize("@ss.hasPermission('business:publicity-education-cls:delete')")
    public CommonResult<Boolean> deletePublicityEducationClsList(@RequestParam("ids") List<Long> ids) {
        publicityEducationClsService.deletePublicityEducationClsListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得宣教分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:publicity-education-cls:query')")
    public CommonResult<PublicityEducationClsRespVO> getPublicityEducationCls(@RequestParam("id") Long id) {
        PublicityEducationClsDO publicityEducationCls = publicityEducationClsService.getPublicityEducationCls(id);
        return success(BeanUtils.toBean(publicityEducationCls, PublicityEducationClsRespVO.class));
    }

    @GetMapping("/all")
    @Operation(summary = "获得所有宣教分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:publicity-education-cls:query')")
    public CommonResult<List<PublicityEducationClsRespVO>> getPublicityEducationClsAll() {
        return success(publicityEducationClsService.getPublicityEducationClsAll());
    }

    @GetMapping("/page")
    @Operation(summary = "获得宣教分类分页")
    @PreAuthorize("@ss.hasPermission('business:publicity-education-cls:query')")
    public CommonResult<PageResult<PublicityEducationClsRespVO>> getPublicityEducationClsPage(@Valid PublicityEducationClsPageReqVO pageReqVO) {
        PageResult<PublicityEducationClsDO> pageResult = publicityEducationClsService.getPublicityEducationClsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PublicityEducationClsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出宣教分类 Excel")
    @PreAuthorize("@ss.hasPermission('business:publicity-education-cls:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPublicityEducationClsExcel(@Valid PublicityEducationClsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PublicityEducationClsDO> list = publicityEducationClsService.getPublicityEducationClsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "宣教分类.xls", "数据", PublicityEducationClsRespVO.class,
                        BeanUtils.toBean(list, PublicityEducationClsRespVO.class));
    }

}